package onlinejudge.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import onlinejudge.constants.Constant;
import onlinejudge.entity.Comment;
import onlinejudge.entity.Community;
import onlinejudge.entity.Reply;
import onlinejudge.service.CommunityCommentService;
import onlinejudge.service.CommunityService;
import onlinejudge.vo.CommunityVo;

@Controller
@RequestMapping("/community")
public class CommunityController {

	@Resource
	private CommunityService communityService;
	@Resource
	private CommunityCommentService communityCommentService;
	
	/**
	 * 社区列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	public String list(HttpServletRequest request) {
		Integer offset=Integer.parseInt(request.getParameter("pager.offset"));
		String queryContent=request.getParameter("queryContent");
		if(queryContent!=null&&queryContent.equals("")) {
			queryContent=null;
		}
		Map<String, Object> parameters=new HashMap<String, Object>();
		parameters.put("queryContent",queryContent);
		//再把搜索内容放回页面上,这样保证了对搜索结果的分页
		request.setAttribute("queryContent", queryContent);
		//在未分页之前先获取搜索结果的数量,也便于对搜索结果的分页
		int resSize=communityService.queryCommunity(parameters).size();
		//分页
		parameters.put("page",offset);
		parameters.put("limit",Constant.getRows());
		List<CommunityVo> communityList=communityService.queryCommunity(parameters);
		
		request.setAttribute("communityList", communityList);
		request.setAttribute("total", resSize);
		request.setAttribute("url", "community/list");
		return "client/community";
	}
	/**
	 * 获取文章item
	 * @param communityId
	 * @param request
	 * @return
	 */
	@RequestMapping("/item")
	public String item(Integer communityId,HttpServletRequest request) {
		Map<String, Object> parameters=new HashMap<String, Object>();
		parameters.put("communityId", communityId);
		request.setAttribute("community", communityService.queryCommunity(parameters).get(0));
		request.setAttribute("commentList", communityCommentService.queryAllComment(communityId));
		request.setAttribute("commentCount", communityCommentService.getCommentCount(communityId));
		return "client/communityItem";
	}
	/**
	 * 写文章页面导向
	 * @return
	 */
	@RequestMapping("/write")
	public String write() {
		return "client/writeCommunity";
	}
	/**
	 * 写文章
	 * @param community
	 * @return
	 */
	@RequestMapping("/writeCommunity")
	public String writeCommunity(Community community) {
		community.setItemContent(community.getItemContent().replace("<br />", "<div style='margin-top:10px;'></div>"));
		communityService.addCommunity(community);
		return "redirect:/community/list?pager.offset=0";
	}
	/**
	 * 更新题解阅读量
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateViewCount")
	public String updateViewCount(@RequestBody Map<String,Object> map) {
		Community community=new Community();
		community.setCommunityId((Integer)map.get("communityId"));
		community.setItemCount(((Integer)map.get("itemCount"))+1);
		communityService.updateCommunity(community);
		JSONObject json=new JSONObject();
		json.put("itemCount",community.getItemCount());
		return json.toJSONString();
	}
	/**
	 * 添加题解的评论接口
	 * @param comment
	 * @param solutionId
	 * @return
	 */
	@RequestMapping("/addComment")
	public String addComment(Comment comment,Integer communityId) {
		communityCommentService.addComment(comment, communityId);
		return "redirect:/community/item?solutionId="+communityId;
	}
	/**
	 * 添加题解评论的回复
	 * @param reply
	 * @param solutionId
	 * @return
	 */
	@RequestMapping("/addReply")
	public String addReply(Reply reply,Integer communityId) {
		communityCommentService.addReply(reply);
		return "redirect:/community/item?communityId="+communityId;
	}
}
